<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Classification
 *
 * Allow the user to manage website classifications
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Classification extends Admin_Controller {
	function Classification( ) {
		parent::Admin_Controller( );

		$this->load->helper( 'form' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'classification' ), 'article/admin/classification' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Classification' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the classification model
		$this->load->model( 'classification_model' );
		
		// Load the language files
		$this->lang->module_load( 'article', 'website' );
		$this->lang->module_load( 'article', 'classification' );
		$this->lang->module_load( 'article', 'domain' );
		$this->lang->module_load( 'article', 'language' );
		$this->lang->module_load( 'article', 'portlets' );
		
		log_message( 'debug', 'conlabz CMS : Classification class loaded' );
	}

	/**
	 * View Classifications
	 *
	 * @access public
	 */
	function index( ) {
		// Get Classification Infomation
		$data['classification'] = $this->classification_model->getClassifications( );
		
		// Display Page
		$data['header'] = $this->lang->line( 'classification_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_admin' ) . "/view_classification";
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
	}

	/**
	 * Set Classification Defaults
	 *
	 * Specify what values should be shown in the fields when creating
	 * a new website by default
	 *
	 * @access private
	 * @todo specify the fields
	 */
	function _set_classification_defaults( ) {
	}
	
	/**
	 * Get Classification Details
	 *
	 * Load user detail values from the submited form
	 *
	 * @access private
	 * @return array
	 */
	function _get_classification_details( ) {
		$data['id']          = $this->input->post( 'id'          );
		$data['title']       = $this->input->post( 'title'       );
		$data['description'] = $this->input->post( 'body_text' );
		
		return $data;
	}
	
	/**
	 * Display Website Form
	 *
	 * @access public
	 * @param integer $id Classification ID
	 */
	function form( $id = NULL ) {    
		// VALIDATION FIELDS
		$fields['id']          = "ID";
		$fields['title']       = $this->lang->line( 'classification_title' );
		$fields['body_text'] = $this->lang->line( 'classification_description' );
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['title']       = "trim|required";
			$rules['body_text'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['title']       = "trim|required";
			$rules['body_text'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$classification = $this->classification_model->getclassifications( array( 'classification.id' => $id ) );
			$classification = $classification->row_array( );
			$this->validation->set_default_value( 'category', $classification['id'] );
			$this->validation->set_default_value( $classification );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category', $this->preference->item( 'default_classification_category' ) );
			// Setup classification defaults
			$this->_set_classification_defaults( );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {		
			$this->validation->output_errors( );
			$data['fckeditor'] = array(
              	'name'        => 'body_text',
              	'id'          => 'body_text',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300'
    		);
			
			$this->validation->output_errors( );
			$data['header'] = ( is_null( $id ) ? $this->lang->line( 'create_classification' ) : $this->lang->line( 'edit_classification' ) );
			$this->bep_site->set_crumb( $data['header'], 'article/admin/classification/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_admin' ) . "/form_classification";
			$data['module'] = 'article';
			$this->load->view( $this->_container, $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$classification = $this->_get_classification_details( );
				$classification['timestamp'] = date( 'Y-m-d H:i:s' );
			
				$this->db->trans_start( );
				$classification['classification_id'] = $this->classification_model->insert( $classification );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $classification['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_user' ) ) );
				}
				redirect( 'article/admin/classification' );
			} else {
				// SAVE
				$classification = $this->_get_classification_details( );
				$classification['timestamp'] = date( 'Y-m-d H:i:s' );
				
				$this->db->trans_start( );
				$this->classification_model->update( $classification, $classification['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $classification['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect( 'article/admin/classification' );
			}
		}
	} // form( );
	
	/**
	 * Delete
	 *
	 * Delete the selected classification from the system
	 *
	 * @access public
	 */
	function delete( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'article/admin/classification', 'location' );
		}

		foreach( $selected as $classification ) {
			$this->db->delete( 'classification', array( 'id' => $classification ) );
		}

		flashMsg( 'success', $this->lang->line( 'classification_deleted' ) );
		redirect( 'article/admin/classification', 'location' );
	}
}
/* End of file classification.php */
/* Location: ./modules/article/controllers/admin/classification.php */